package test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class SplitImage {
	static String[] postfix = { "normal", "rollover", "pressed", null, "disabled" };

	public static void main(String[] args) {
		try {
			BufferedImage img = read(new File("c:\\restore.png"));
			BufferedImage[] parts = split(img, 5);
			save(parts, "restore");
			System.out.println("Complete");
		} catch (Exception e) {
			System.out.println("ERROR:");
			e.printStackTrace();
		}
	}

	static BufferedImage read(File file) throws IOException {
		return ImageIO.read(file);
	}

	static BufferedImage[] split(BufferedImage source, int part) {
		int width = source.getWidth();
		int step = width / part;
		BufferedImage[] imgs = new BufferedImage[part];
		int height = source.getHeight();
		for (int i = 0; i < part; i++)
			imgs[i] = source.getSubimage(i * step, 0, step, height);
		return imgs;
	}

	static void save(BufferedImage[] imgs, String name) throws IOException {
		File dir = new File("c:\\");
		for (int i = 0; i < imgs.length; i++) {
			if (null == postfix[i])
				continue;
			File file = new File(dir, name + "-" + postfix[i] + ".png");
			ImageIO.write(imgs[i], "png", file);
		}
	}
}
